package priv.pront.code.mati.national.practice;

import java.util.Scanner;

/**
 * @Author: Pronting
 * @Description: TODO
 * @DateTime: 2023/12/1 15:08
 **/
public class P1596_Lake_Counting_S {

    static int n, m, count = 0;
    static char[][] g;
    static boolean[][] st;
    static int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1}, dy = {0, 1, 1, 1, 0, -1, -1, -1};
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        g = new char[n][m];
        st = new boolean[n][m];

        for (int i = 0; i < n; i++) {
            g[i] = scanner.next().toCharArray();
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(!st[i][j] && g[i][j] == 'W'){
                    dfs(i, j);
                    count++;
                }
            }
        }
        System.out.println(count);
    }

    private static void dfs(int row, int col){
        for (int i = 0; i < 8; i++) {
            int nx = dx[i] + row, ny = dy[i] + col;
            if(nx < 0 || nx >= n || ny < 0 || ny >= m || st[nx][ny] || g[nx][ny] == '.') continue;
            st[nx][ny] = true;
            dfs(nx, ny);
        }
    }
}
